﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.CarInsurance;

public partial class carinsurance_ViewCallSummary : System.Web.UI.Page
{
    int _currentRow = 0;
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            lblError.Text = "";
            lblContent.Text = "";
            ddlDay1.Visible = true;
            lblFromDate.Text = Resources.General.Date + " : ";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
            ddlYear1.Items.Clear();
            ddlYear2.Items.Clear();

            int _cryr = dateTimeNow.Year;
            int _styr = 2010;
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
                ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }

            ddlDay1.Text = dateTimeNow.Day.ToString("00");
            ddlMonth1.Text = dateTimeNow.Month.ToString("00");
            ddlYear1.Text = dateTimeNow.Year.ToString("0000");
            ddlDay2.Text = dateTimeNow.Day.ToString("00");
            ddlMonth2.Text = dateTimeNow.Month.ToString("00");
            ddlYear2.Text = dateTimeNow.Year.ToString("0000");
        }

        if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "100")
        {
            ibtSubmit.Visible = false;
            
            ddlCallType.Enabled = false;
            ddlReportType.Enabled = false;
            grvCallSummary.Visible = false;
            ddlDay1.Enabled = false;
            ddlMonth1.Enabled = false;
            ddlYear1.Enabled = false;
            lblError.Text = Resources.CarInsurance.YourAreNotAuthorizedToViewPage;
        }
    }

    protected void ibtSubmit_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        txtReportType.Text = ddlReportType.Text;
        txtAgentId.Text = Session[CE.Global.General.UserId].ToString();
        LoadGrid();
    }

    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        ibtSubmit_Click(sender, e);
        GenerateExcel();
    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblError.Text = "";
        ddlDay1.Text = dateTimeNow.Day.ToString("00");
        ddlDay2.Text = dateTimeNow.Day.ToString("00");
        ddlMonth1.Text = dateTimeNow.Month.ToString("00");
        ddlMonth2.Text = dateTimeNow.Month.ToString("00");
        ddlYear1.Text = dateTimeNow.Year.ToString("0000");
        ddlYear2.Text = dateTimeNow.Year.ToString("0000");
        if (Convert.ToInt16(ddlReportType.Text) == 0)
        {
            ddlDay1.Visible = false;
            lblFromDate.Text = Resources.General.Date + " : ";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = Resources.General.Date + " : ";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            lblFromDate.Text = " " + Resources.General.FromDate + " : ";
            lblToDate.Text = " " + Resources.General.ToDate + " : ";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlYear2.Visible = true;
            ddlDay1.Visible = true;
        }
    }

    protected void grvCallSummary_SelectedIndexChanged(object sender, EventArgs e)
    {
        ibtSubmit_Click(sender, e);
    }

    public bool ValidDate(Int32 Year, Int32 Month, Int32 Day)
    {
        bool _isValid = true;
        int daysInMonth = 0;
        string leap, monthValid, dayValid, yearValid;
        if (Year >= 0000)
        {
            yearValid = "yes";
        }
        else
        {
            yearValid = "no";
        }
        if (Year % 400 == 0)
        {
            leap = "yes";
        }
        else if (Year % 100 == 0)
        {
            leap = "no";
        }
        else if (Year % 4 == 0)
        {
            leap = "yes";
        }
        else
        {
            leap = "no";
        }

        switch (Month)
        {
            case 1: daysInMonth = 31;
                break;
            case 3: daysInMonth = 31;
                break;
            case 4: daysInMonth = 30;
                break;
            case 5: daysInMonth = 31;
                break;
            case 6: daysInMonth = 30;
                break;
            case 7: daysInMonth = 31;
                break;
            case 8: daysInMonth = 31;
                break;
            case 9: daysInMonth = 30;
                break;
            case 10: daysInMonth = 31;
                break;
            case 11: daysInMonth = 30;
                break;
            case 12: daysInMonth = 31;
                break;
        }

        if (Month >= 1 && Month <= 12)
        {
            monthValid = "yes";
        }
        else
        {
            monthValid = "no";
        }
        if (Month == 2)
        {
            if (leap == "no")
            {
                daysInMonth = 28;
            }
            else if (leap == "yes")
            {
                daysInMonth = 29;
            }
        }

        if (1 <= Day && Day <= daysInMonth)
        {
            dayValid = "yes";
        }

        else
        {
            dayValid = "no";
        }

        if ((dayValid == "yes") && (monthValid == "yes") && (yearValid == "yes"))
        {
            _isValid = true;
        }
        else
        {
            _isValid = false;
        }

        return _isValid;
    }

    public string GetMonth(string month)
    {
        string result = "";
        if (month == "01")
        {
            result = " Jan ";
        }
        else if (month == "02")
        {
            result = " Feb ";
        }
        else if (month == "03")
        {
            result = " Mar ";
        }
        else if (month == "04")
        {
            result = " Apr ";
        }
        else if (month == "05")
        {
            result = " May ";
        }
        else if (month == "06")
        {
            result = " Jun ";
        }
        else if (month == "07")
        {
            result = " Jul ";
        }
        else if (month == "08")
        {
            result = " Aug ";
        }
        else if (month == "09")
        {
            result = " Sep ";
        }
        else if (month == "10")
        {
            result = " Oct ";
        }
        else if (month == "11")
        {
            result = " Nov ";
        }
        else if (month == "12")
        {
            result = " Dec ";
        }
        return result;
    }

    public bool Validatioin()
    {
        lblError.Text = "";
        bool _isValid = true;
        DateTime _fromDate, _toDate;
        if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            if (ddlDay1.SelectedIndex >= 0)
            {
                if (ValidDate(Convert.ToInt32(ddlYear1.Text), Convert.ToInt32(ddlMonth1.Text), Convert.ToInt32(ddlDay1.Text)))
                {
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(ddlYear1.Text) + "/" + Convert.ToInt32(ddlMonth1.Text) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (_fromDate > dateTimeNow)
                    {
                        _isValid = false;
                        lblError.Text = Resources.General.ErrDatelessorequalToday;
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = Resources.General.ErrSelectValidDate;
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = Resources.General.ErrSelectDate;
            }
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            if (ddlDay1.SelectedIndex >= 0 && ddlDay2.SelectedIndex >= 0)
            {
                if (ValidDate(Convert.ToInt32(ddlYear1.Text), Convert.ToInt32(ddlMonth1.Text), Convert.ToInt32(ddlDay1.Text)))
                {
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(ddlYear1.Text) + "/" + Convert.ToInt32(ddlMonth1.Text) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (ValidDate(Convert.ToInt32(ddlYear2.Text), Convert.ToInt32(ddlMonth2.Text), Convert.ToInt32(ddlDay2.Text)))
                    {
                        _toDate = Convert.ToDateTime(Convert.ToInt32(ddlYear2.Text) + "/" + Convert.ToInt32(ddlMonth2.Text) + "/" + Convert.ToInt32(ddlDay2.Text));
                        if (_fromDate <= dateTimeNow)
                        {
                            if (_fromDate > _toDate)
                            {
                                _isValid = false;
                                lblError.Text = Resources.General.ErrFromdatelessthanTodate;
                            }
                        }
                        else
                        {
                            _isValid = false;
                            lblError.Text = Resources.General.ErrFromdatelessthanToday;
                        }
                    }
                    else
                    {
                        _isValid = false;
                        lblError.Text = Resources.General.ErrSelectValidTodate;
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = Resources.General.ErrSelectValidFromdate;
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = Resources.General.ErrSelectFromandTodate;
            }
        }
        return _isValid;
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        if (Validatioin() == true)
        {            
            txtReportDate1.Text = "";
            txtReportDate2.Text = dateTimeNow.Year.ToString("0000") + dateTimeNow.Month.ToString("00") + dateTimeNow.Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                txtReportDate2.Text = ddlYear2.Text;
                txtReportDate2.Text += ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }
            DataTable _dtSummary = new DataTable();
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate1 = txtReportDate1.Text.Trim();
            string _ReportDate2 = txtReportDate2.Text.Trim();
            string _RefCEUserId = Session[CE.Global.General.UserId].ToString();
            Int16 _CallType = Convert.ToInt16(ddlCallType.Text);
            Int16 _Language = Convert.ToInt16(lblLangOption.Text);

            MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
            string _spName = "[sprocCXCRMAgentCallStatusSummarySelectedList]";
            SortedList _sPara = new SortedList();

            _sPara.Add("@ReportType", _ReportType);
            _sPara.Add("@ReportDate1", _ReportDate1);
            _sPara.Add("@ReportDate2", _ReportDate2);
            _sPara.Add("@CallType", _CallType);
            _sPara.Add("@Language", _Language);

            _dtSummary = _db.GetDataTableBySP(_spName, _sPara);
            if (_dtSummary != null && _dtSummary.Rows.Count > 0)
            {
                lblError.Text = "";
                grvCallSummary.DataSource = _dtSummary;
                grvCallSummary.DataBind();
                EnablePrintView();
            }
            else
            {
                grvCallSummary.DataSource = null;
                grvCallSummary.DataBind();
                EnablePrintView();
            }
        }
        else
        {
            grvCallSummary.DataSource = null;
            grvCallSummary.DataBind();
            EnablePrintView();
        }
    }

    private void EnablePrintView()
    {
        if (grvCallSummary.Rows.Count > 0)
        {
            btnPrint.Visible = true;
            btnDownLoad.Visible = true;
            lblRecordCount.Text = "(" + grvCallSummary.Rows.Count.ToString() + ") record(s). Date/Time: " + dateTimeNow.ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
        }
        else
        {
            btnPrint.Visible = false;
            btnDownLoad.Visible = false;
            lblRecordCount.Text = "No record found. Date/Time: " + dateTimeNow.ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
        }
    }

    public void GenerateExcel()
    {
        string _html = "";
        string _fileName = "CallStatus@" + dateTimeNow.Day.ToString("00") + "-" + dateTimeNow.Month.ToString("00") + "-" + dateTimeNow.Year;
        lblContent.Text = "";
        lblError.Text = "";
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = dateTimeNow.Year.ToString("0000") + dateTimeNow.Month.ToString("00") + dateTimeNow.Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                txtReportDate2.Text = ddlYear2.Text;
                txtReportDate2.Text += ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }

            DataTable _dtSummary = new DataTable();
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate1 = txtReportDate1.Text.Trim();
            string _ReportDate2 = txtReportDate2.Text.Trim();
            string _RefCEUserId = Session[CE.Global.General.UserId].ToString();
            Int16 _Language = Convert.ToInt16(lblLangOption.Text);
            Int16 _CallType = Convert.ToInt16(ddlCallType.Text);

            MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
            string _spName = "[sprocCXCRMAgentCallStatusReportSummary]";
            SortedList _sPara = new SortedList();

            _sPara.Add("@ReportType", _ReportType);
            _sPara.Add("@ReportDate1", _ReportDate1);
            _sPara.Add("@ReportDate2", _ReportDate2);
            _sPara.Add("@CallType", _CallType);
            _sPara.Add("@Language", _Language);

            _dtSummary = _db.GetDataTableBySP(_spName, _sPara);
            if (_dtSummary != null && _dtSummary.Rows.Count > 0)
            {
                _html = _dtSummary.Rows[0]["Reports"].ToString();
                lblContent.Text = _html;
                //_fileName += ".xls";
                ExportGridView(_fileName, lblContent);
            }
        }
        else
        {
        }
    }

    public void ExportGridView(string fileName, Label lblContent)
    {
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        Response.Write(lblContent.Text);
        Response.End();
    }

}
